home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / Think Class Libraries / Intelligent classes 1.0 / CIntelligentWindow.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-11-30  |  5.6 KB  |  210 lines  |  [TEXT/KAHL]

  1. /****
  2.  *    CIntelligentWindow.c
  3.  *
  4.  *    This class makes the Window a little more intelligent.
  5.  *
  6.  *    Copyright © 1993 Quipus, by Mårten Sörliden.  All rights reserved.
  7.  *
  8.  ****/
  9.  
  10. #include <TBUtilities.h>
  11. #include <Packages.h>
  12. #include <Constants.h>
  13. #include <CList.h>
  14. #include <CIntelligentWindow.h>
  15. #include <CIntelligentDecorator.h>
  16. #include <CIntelligentScrollPane.h>
  17.  
  18. #define     kMinWindWidth        125
  19. #define     kMinWindHeight        88        // = 125 / Sqrt(2)
  20.  
  21. #define     kLeftZoomSizeOffset            66
  22. #define     kTopZoomSizeOffset            47
  23. #define     kRightZoomSizeOffset        -7
  24. #define     kBottomZoomSizeOffset        -25
  25.  
  26. /*** Global Variables ***/
  27.  
  28. extern CDecorator *gDecorator;
  29.  
  30.  
  31. /** Construction and destruction methods **/
  32.  
  33. void CIntelligentWindow::IIntelligentWindow(short WINDid, Boolean aFloating, CDesktop *anEnclosure, CDirector *aSupervisor)
  34. {
  35.     IWindow(WINDid, aFloating, anEnclosure, aSupervisor);
  36.     sizeRect.left = kMinWindWidth;
  37.     sizeRect.top = kMinWindHeight;
  38.     wNum = 0;
  39.     stdStateChanged = FALSE;
  40.     lastDirection = 0;
  41. }
  42.  
  43. void CIntelligentWindow::Dispose(void)
  44. {
  45.  
  46.     inherited::Dispose();
  47. }
  48.  
  49.  
  50. /** Accessing methods **/
  51.  
  52. // override
  53. void    CIntelligentWindow::SetTitle(Str255 theTitle)
  54. {
  55.     Str255 wTitle;
  56.     Rect portRect;
  57.     Rect portBitBounds;
  58.  
  59.     GetTitle(wTitle);
  60.     if (!IUEqualString(wTitle, theTitle))
  61.         wNum = 0;
  62. // The portRect is saved and restored. 
  63. // If the title of the window is named ie '92-06-19' the size of the window will be changed.
  64.     portRect = (*macPort).portRect;
  65.     portBitBounds = (*macPort).portBits.bounds;
  66.     inherited::SetTitle(theTitle);
  67.     (*macPort).portRect = portRect;
  68.     (*macPort).portBits.bounds = portBitBounds;
  69. }
  70.  
  71. void CIntelligentWindow::SetDefaultTitle()
  72. {
  73.     Str255        wTitle;
  74.     Str255        wNumber;    
  75.  
  76.     if (wNum == 0)
  77.         if (member(gDecorator, CIntelligentDecorator))
  78.             wNum = ((CIntelligentDecorator*)gDecorator)->GetWNum();
  79.         else
  80.             wNum = gDecorator->GetWCount();
  81.     GetTitle(wTitle);
  82.     NumToString(wNum, wNumber);
  83.     ConcatPStrings(wTitle, (StringPtr) "\p ");
  84.     ConcatPStrings(wTitle, wNumber);
  85.     inherited::SetTitle(wTitle);
  86. }
  87.  
  88. short CIntelligentWindow::GetWNum()
  89. {
  90.     return(wNum);
  91. }
  92.  
  93. void    CIntelligentWindow::SetStdState(Rect *aStdState)
  94. {
  95.     WStateData    **zoomData;
  96.     int leftMargin, topMargin;
  97.     Rect zoomSizeRect;
  98.  
  99.     zoomData = (WStateData**) ((WindowPeek)macPort)->dataHandle;
  100.     leftMargin = LEFT_SMARGIN - 1;
  101.     topMargin = TOP_SMARGIN + GetMBarHeight() - 1;
  102.     zoomSizeRect = sizeRect;
  103.     SetRect(&zoomSizeRect, sizeRect.left + kLeftZoomSizeOffset, sizeRect.top + kTopZoomSizeOffset, 
  104.                                     sizeRect.right + kRightZoomSizeOffset, sizeRect.bottom + kBottomZoomSizeOffset);
  105. // Change the size of aStdState if neccecray
  106.     if (aStdState->right - aStdState->left < zoomSizeRect.left)
  107.         aStdState->right = aStdState->left + zoomSizeRect.left;
  108.     if (aStdState->right - aStdState->left > zoomSizeRect.right)
  109.         aStdState->right = aStdState->left + zoomSizeRect.right;
  110.     if (aStdState->bottom - aStdState->top < zoomSizeRect.top)
  111.         aStdState->bottom = aStdState->top + zoomSizeRect.top;
  112.     if (aStdState->bottom - aStdState->top > zoomSizeRect.bottom)
  113.         aStdState->bottom = aStdState->top + zoomSizeRect.bottom;
  114.     if ((leftMargin > aStdState->left) || (topMargin > aStdState->top) || 
  115.         (leftMargin + zoomSizeRect.right < aStdState->right) ||
  116.         (topMargin + zoomSizeRect.bottom < aStdState->bottom)) {
  117. // Move window to upper left corner
  118.         aStdState->right = aStdState->right + leftMargin - aStdState->left;
  119.         aStdState->left = leftMargin;
  120.         aStdState->bottom = aStdState->bottom + topMargin - aStdState->top;
  121.         aStdState->top = topMargin;
  122.     }
  123.     SetPort(macPort);
  124.     ForceNextPrepare();
  125.     if (!EqualRect(&(**zoomData).stdState, aStdState)) {
  126.         (**zoomData).stdState = *aStdState;
  127.         stdStateChanged = TRUE;
  128.     }
  129. }
  130.  
  131.  
  132. /** Size and location methods **/
  133.  
  134. // Override
  135. void    CIntelligentWindow::Drag(EventRecord *macEvent)
  136. {
  137.     inherited::Drag(macEvent);
  138.     UpdateUserState();
  139. }
  140.  
  141. // Override
  142. void    CIntelligentWindow::Resize(EventRecord *macEvent)
  143. {
  144.     inherited::Resize(macEvent);
  145.     UpdateUserState();
  146. }
  147.  
  148. // Override
  149. void CIntelligentWindow::Zoom(short direction)
  150. {
  151.     WStateData **zoomData;
  152.  
  153.     zoomData = (WStateData**) ((WindowPeek)macPort)->dataHandle;
  154.     if (EqualRect(&(**zoomData).userState, &(**zoomData).stdState))
  155.         return;
  156.     if ((lastDirection == inZoomIn) && (direction == inZoomIn))
  157.         return;
  158.     if ((stdStateChanged) && (direction == inZoomIn))
  159.         direction = inZoomOut;
  160.     inherited::Zoom(direction);
  161.     stdStateChanged = FALSE;
  162.     lastDirection = direction;
  163. }
  164.  
  165. void CIntelligentWindow::UpdateStdState()
  166. {
  167.     WStateData **zoomData;
  168.     Rect theStdState;
  169.     CView *theView;
  170.     long n, i;
  171.     LongRect unionFrame;
  172.     LongRect theFrame;
  173.     
  174.     if (((WindowPeek)macPort)->spareFlag) {
  175.                                         /* Get standard state rectangle        */
  176.         zoomData = (WStateData**) ((WindowPeek)macPort)->dataHandle;
  177.         SetLongRect(&unionFrame, 0L, 0L, 0L, 0L);
  178.         n = itsSubviews->GetNumItems();
  179.         for (i = 1; i <= n; i++) {
  180.             theView = (CView *)itsSubviews->NthItem(i);
  181.             if (member(theView, CIntelligentScrollPane)) {
  182.                 ((CIntelligentScrollPane *)theView)->GetPreferedFrame(&theFrame);
  183.                 UnionLongRect(&theFrame, &unionFrame, &unionFrame);
  184.             }
  185.         }
  186.         if (!EmptyLongRect(&unionFrame)) {
  187.             theStdState = (**zoomData).userState;
  188.             theStdState.right = unionFrame.right + theStdState.left - 2;
  189.             theStdState.bottom = unionFrame.bottom + theStdState.top - 2;
  190.             SetStdState(&theStdState);
  191.         }
  192.     }
  193. }
  194.  
  195. void CIntelligentWindow::UpdateUserState()
  196. {
  197.     Rect windowRect;
  198.     WStateData **zoomData;
  199.  
  200.     if (((WindowPeek)macPort)->spareFlag) {
  201.         zoomData = (WStateData**) ((WindowPeek)macPort)->dataHandle;
  202.         SetPort(macPort);
  203.         ForceNextPrepare();
  204.         windowRect = macPort->portRect;
  205.         LocalToGlobal(&topLeft(windowRect));
  206.         LocalToGlobal(&botRight(windowRect));
  207.         (**zoomData).userState = windowRect;
  208.     }
  209. }
  210.